package com.chanyachting.emall.action.opertionmenu;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Opertionmenu;
import com.chanyachting.emall.form.OpertionmenuForm;
import com.chanyachting.emall.util.RequestUtil;

public class OpertionmenuAction extends DispatchAction {

	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil  ru=new RequestUtil(request);
		
		ru.setAttribute("opertionmenuList", Opertionmenu.list());
		
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil  ru=new RequestUtil(request);
		
		if(request.getMethod().equals("POST")){
			
			OpertionmenuForm omForm=(OpertionmenuForm)form;
			
			if(omForm.validate(mapping, request).isEmpty()){
				
				Opertionmenu om=new Opertionmenu();
				
				BeanUtils.copyProperties(om, omForm);
				
				if(om.save()){
					
					ru.setSuccessMsg();
					
				}else{
					
					ru.setFailMsg();
				}
					
			}
			
		}
		
		
		return mapping.findForward("create");
	}
	
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil  ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			OpertionmenuForm omForm=(OpertionmenuForm)form;
			
			if(omForm.validate(mapping, request).isEmpty()){
				
				Opertionmenu om=new Opertionmenu(id);
				
				BeanUtils.copyProperties(om, omForm);
				
				if(om.update()){
					
					ru.setSuccessMsg();
					
				}else{
					
					ru.setFailMsg();
				}
					
			}
			
		}
		
		ru.setAttribute("opertionmenu", new Opertionmenu(id));
		
		return mapping.findForward("update");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil  ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Opertionmenu om=new Opertionmenu(id);
		
		om.delete();
		
		return mapping.findForward("delete");
	}
	
	
	

}
